package com.ck.es.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.ck.es.prop.EsProp;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ck
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.elasticsearch",name = "enable",havingValue = "true")
public class ElasticsearchClientConfig {


    @Resource
    private EsProp esProp;


//    /***
//     * RestHighLevelClient客户端
//     * @return
//     */
//    @Bean
//    public RestHighLevelClient client() {
//        //es 账密连接
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esProp.getUsername(), esProp.getPassword()));
//
//        // 拆分地址
//        List<HttpHost> hostLists = new ArrayList<>();
//        String[] hostArray = esProp.getHosts().split(",");
//        for (String temp : hostArray) {
//            String host = temp.split(":")[0];
//            String port = temp.split(":")[1];
//            hostLists.add(new HttpHost(host, Integer.parseInt(port), esProp.getSchema()));
//        }
//
//        // 转换成 HttpHost 数组
//        HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
//        // 构建连接对象
//        RestClientBuilder builder = RestClient.builder(httpHost);
//        // 异步连接延时配置
//        builder.setRequestConfigCallback(requestConfigBuilder -> {
//            requestConfigBuilder.setConnectTimeout(esProp.getConnectTimeOut());
//            requestConfigBuilder.setSocketTimeout(esProp.getSocketTimeOut());
//            requestConfigBuilder.setConnectionRequestTimeout(esProp.getConnectionRequestTimeOut());
//            return requestConfigBuilder;
//        });
//
//        // 异步连接数配置
//        builder.setHttpClientConfigCallback(httpClientBuilder -> {
//            httpClientBuilder.setMaxConnTotal(esProp.getMaxConnectNum());
//            httpClientBuilder.setMaxConnPerRoute(esProp.getMaxConnectPerRoute());
//            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//            return httpClientBuilder;
//        });
//
//        return new RestHighLevelClient(builder);
//
//    }


    /**
     * ElasticsearchClient客户端
     * @return
     */
    @Bean
    public RestClient restClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esProp.getUsername(), esProp.getPassword()));

        // 拆分地址
        List<HttpHost> hostLists = new ArrayList<>();
        String[] hostArray = esProp.getHosts().split(",");
        for (String temp : hostArray) {
            String host = temp.split(":")[0];
            String port = temp.split(":")[1];
            hostLists.add(new HttpHost(host, Integer.parseInt(port), esProp.getSchema()));
        }

        // 转换成 HttpHost 数组
        HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
        // 构建连接对象
        RestClientBuilder builder = RestClient.builder(httpHost);
        // 异步连接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder -> {
            requestConfigBuilder.setConnectTimeout(esProp.getConnectTimeOut());
            requestConfigBuilder.setSocketTimeout(esProp.getSocketTimeOut());
            requestConfigBuilder.setConnectionRequestTimeout(esProp.getConnectionRequestTimeOut());
            return requestConfigBuilder;
        });

        // 异步连接数配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            httpClientBuilder.setMaxConnTotal(esProp.getMaxConnectNum());
            httpClientBuilder.setMaxConnPerRoute(esProp.getMaxConnectPerRoute());
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            return httpClientBuilder;
        });

        return builder.build();
    }

    /**
     * 使用Jackson映射器创建传输层
     * @param restClient
     * @return
     */
    @Bean
    public ElasticsearchTransport elasticsearchTransport (RestClient restClient) {
        return new RestClientTransport(
                restClient, new JacksonJsonpMapper());
    }

    @Bean
    public ElasticsearchClient elasticsearchClient (ElasticsearchTransport transport) {
        return new ElasticsearchClient(transport);
    }
}